package com.rongshu.view.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.tools.ScreenUtils;
import com.rongshu.R;
import com.rongshu.view.home.listener.OnItemClickListener;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.widget.CircleImageView;

import java.util.List;


public class SharePenPalWillAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<PenpalResponse.ResponseBean> sharePenpalwillInfos;

    //interface
    private OnItemClickListener mOnItemClickListener = null;

    public SharePenPalWillAdapter(Context context) {
        this.mContext = context;
    }

    public void setSharePenpalwillInfos(List<PenpalResponse.ResponseBean> list) {
        this.sharePenpalwillInfos = list;
    }

    public List<PenpalResponse.ResponseBean> getSharePenpalwillInfos() {
        return this.sharePenpalwillInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_share_pen_pal_will_item_view, parent, false);
        return new SharePenPalWillViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        PenpalResponse.ResponseBean responseBean = sharePenpalwillInfos.get(position);
        SharePenPalWillViewHolder viewHolder = (SharePenPalWillViewHolder) holder;
        //定义item宽度
        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
        layoutParams.width = (ScreenUtils.getScreenWidth(mContext) / 5);
        viewHolder.itemView.setLayoutParams(layoutParams);
        //avatar
        RequestOptions options = new RequestOptions()
                .fitCenter()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(mContext)
                .load(responseBean.getImg())
                .apply(options)
                .into(viewHolder.iv_head_icon);
        String penclubname=responseBean.getPenclubname();
        if (penclubname.length() > 5) {
            viewHolder.txt_pen_name.setText(penclubname.substring(0, 5) + "...");
        } else {
            viewHolder.txt_pen_name.setText(penclubname);
        }
        viewHolder.iv_select.setVisibility(responseBean.getIsselect() ? View.VISIBLE : View.GONE);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (sharePenpalwillInfos == null) {
            return 0;
        }
        return sharePenpalwillInfos.size();
    }

    public static class SharePenPalWillViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView iv_head_icon;

        private ImageView iv_select;

        private TextView txt_pen_name;

        public SharePenPalWillViewHolder(View itemView) {
            super(itemView);
            iv_head_icon = itemView.findViewById(R.id.iv_head_icon);
            iv_select = itemView.findViewById(R.id.iv_select);
            txt_pen_name = itemView.findViewById(R.id.txt_pen_name);
        }
    }

    //set litener
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
